## packages
library(readr)
library(dplyr)
library(forcats)
library(purrr)
library(raster)
library(ggplot2)
library(ggh4x)
library(ggtext)
library(scico)
library(patchwork)
library(here)
## plot theme
theme_set(d6::theme_d6(base_size = 18, legend = "bottom"))
theme_update(
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(color = "#fefefe", linewidth = .5),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "grey95", color = "grey95"),
panel.border = element_rect(fill = "transparent", color = "transparent"),
panel.spacing.x = unit(.9, "lines"),
panel.spacing.y = unit(1.5, "lines"),
legend.justification = "left",
legend.box.margin = margin(-20, 0, 0, 0),
legend.text = element_text(family = "PT Sans Narrow"),
axis.text = element_text(family = "PT Mono", size = rel(.7)),
axis.text.x = element_text(margin = margin(t = 5)),
axis.text.y = element_text(margin = margin(r = 5)),
axis.text.y.right = element_text(hjust = 1),
axis.ticks.length = unit(0, "lines"),
strip.text = element_text(color = "black"),
strip.text.x = element_markdown(face = "plain", vjust = 0, lineheight = 1.05),
strip.text.y.left = element_markdown(angle = 0, hjust = 0, vjust = .5),
strip.background = element_rect(color = "transparent", fill = "transparent"),
strip.placement = "outside",
plot.title = element_markdown(),
plot.subtitle = element_text(hjust = .5, size = rel(1.12)),
plot.margin = margin(rep(1.5, 4))
)
## path + folder for plots
dir_plots <- here("plots")
if (!dir.exists(dir_plots)) dir.create(dir_plots, recursive = TRUE)The 4th Dimension of Movement
Manuscript Figures (simulated tracks)
Preparation
Import Simulated Landscapes
lscp_elv <- read_rds(here("data", "simulated", "landscapes", "1", "cont.rds"))
lscp_hab <- read_rds(here("data", "simulated", "landscapes", "1", "cat.rds"))
lscp_hr <- read_rds(here("data", "simulated", "landscapes", "hr.rds"))
lscp_mix <-
list(
calc(stack(lscp_elv[[1]], lscp_hab[[1]]), mean),
calc(stack(lscp_elv[[2]], lscp_hab[[2]]), mean),
calc(stack(lscp_elv[[3]], lscp_hab[[3]]), mean),
calc(stack(lscp_elv[[3]], lscp_hab[[1]]), mean),
calc(stack(lscp_elv[[1]], lscp_hab[[3]]), mean)
)Import Simulation Results
dat_est <- read_rds(here("output", "simulated", "results_combined.rds"))Plot Settings
colors <- c(`Elevation (numeric)` = "#CD9804", `Habitat (categorical)` = "#1B9E77")
width <- 1.2
spacing <- .6Visualizations
Figure 1
Raw maps for conceptual figure
plot_map <- function(raster, layer, titles = c("low", "intermediate", "high")) {
dat <-
raster[[layer]] |>
raster::rasterToPoints() |>
as_tibble()
if (names(dat)[3] == "var1") var <- "elevation"
if (names(dat)[3] == "var2") var <- "habitat"
if (names(dat)[3] == "layer") var <- "combined"
if (names(dat)[3] == "d") var <- "directionality"
if (var == "habitat") dat <- mutate(dat, var2 = factor(var2, levels = c(1, 0), labels = c("Habitat", "Matrix")))
title <- titles[layer]
# if (layer == 1) { title <- "low" }
# if (layer == 2) { title <- "intermediate" }
# if (layer == 3) { title <- "high" }
map <-
dat |>
dplyr::select("x" = 1, "y" = 2, "z" = 3) |>
ggplot(aes(x, y, fill = z)) +
geom_raster() +
scale_x_continuous(name = NULL, labels = scales::label_comma()) +
scale_y_continuous(name = NULL, labels = scales::label_comma()) +
coord_equal(clip = "off", expand = FALSE) +
labs(subtitle = title) +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
legend.position = "bottom",
legend.direction = "vertical",
plot.subtitle = element_text(margin = margin(15, 1, -5, 1)),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
#axis.text = element_text(margin = margin(t = 12), family = "PT Mono"),
#axis.text.x = element_text(margin = margin(t = 12)),
#axis.text.y = element_text(margin = margin(r = 12)),
axis.ticks.length = unit(0, "lines"),
plot.margin = margin(rep(1, 4))
)
if (var == "elevation") {
map <- map +
#scale_fill_scico(palette = "bamako", midpoint = .5, limits = c(0, 1), name = NULL) +
scale_fill_gradientn(
colors = c("#feedbf", "#fde298", "#fcd872", "#fbcd4b", "#fbc225", "#f3b505", "#cd9804", "#a77b03", "#805f02", "#5a4202"),
name = NULL
) +
theme(
legend.text = element_text(family = "PT Mono"),
legend.key.width = unit(7.5, "lines"), legend.key.height = unit(.75, "lines"),
legend.direction = "horizontal", legend.ticks = element_blank()
)
}
if (var == "habitat") {
map <- map +
scale_fill_manual(values = c("#00664A", "#E0DDC9"), name = NULL) +
theme(legend.text = element_text(family = "PT Sans Narrow", size = rel(1)),
legend.key.spacing.y = unit(.2, "lines"))
}
if (var == "combined") {
map <- map +
scale_fill_gradientn(
colors = c('#ffea9b', '#e0d689', '#c0c27e', '#a1af72', '#839c67', '#65895a', '#49764d', '#2b6340', '#005133'),
limits = c(0, 1), name = NULL
) +
theme(
legend.text = element_text(family = "PT Mono"), legend.justification = "center",
legend.key.width = unit(7.5, "lines"), legend.key.height = unit(.75, "lines"),
legend.direction = "horizontal", legend.ticks = element_blank(),
plot.subtitle = element_markdown()
)
}
if (var == "directionality") {
lims <- range(dat$d)
map <- map +
scico::scale_fill_scico(
palette = "lapaz", direction = -1, name = NULL, limits = lims,
breaks = lims, labels = c("high", "low")
) +
theme(
legend.text = element_text(family = "PT Sans Narrow"), legend.justification = "center",
legend.key.width = unit(2.5, "lines"), legend.key.height = unit(.75, "lines"),
legend.direction = "horizontal", legend.ticks = element_blank(),
plot.subtitle = element_markdown()
)
}
return(map)
}list_elv <- map(1:3, ~plot_map(lscp_elv, .x))
maps_elv <-
list_elv[[1]] + plot_spacer() + list_elv[[2]] + plot_spacer() + list_elv[[3]] +
plot_layout(guides = "collect", widths = c(1, .1, 1, .1, 1)) +
plot_annotation(title = "**Elevation** (numeric)",
theme = theme(legend.box.margin = margin(-5, 1, -5, 5),
plot.title = element_markdown(face = "plain", hjust = 0),
plot.title.position = "plot"))
ggsave(paste0(dir_plots, "/_raw/1_s1_sim_maps_elv_1.png"), width = 8, height = 4.7, dpi = 600)
maps_elv